package com.ambulance.framework.task;

import com.ambulance.project.biz.dto.admin.StaticItem;
import com.ambulance.project.biz.entity.Statistics;
import com.ambulance.project.biz.mapper.MyCustomerMapper;
import com.ambulance.project.biz.service.StatisticsService;
import com.ambulance.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/***
 *  @description
 *  @author zhao
 *  @date: 2020/5/22
 *  @version: v1.0
 */
@Component("staticsTask")
public class StaticsTask {


    @Autowired
    private MyCustomerMapper myCustomerMapper;
    @Autowired
    private StatisticsService statisticsService;

    /**
     * 统计上一天车辆救护信息
     */
    public void doStaticsCarsMsg() {

        //统计前一日的出车次数
        //救助时长
        //出车时长
        LocalDate localDate = LocalDate.now().minusDays(1);

        List<StaticItem> dispatcherNums = myCustomerMapper.queryLastDayDispatcherNum(localDate);
        List<StaticItem> onCarTimes = myCustomerMapper.queryLastDayOnCarTime(localDate);
        List<StaticItem> onHelpTimes = myCustomerMapper.queryLastDayOnHelpTime(localDate);
        if(CommonUtils.checkNull(dispatcherNums)){
            Statistics statistics = new Statistics();
            statistics.setTime(0);
            statistics.setAvgCar(new BigDecimal(0));
            statistics.setAvgBaseHelp(new BigDecimal(0));
            statistics.setMonth(localDate.getMonthValue());
            statistics.setDay(localDate.getDayOfMonth());
            statistics.setYear(localDate.getYear());
            statistics.setCreateTime(LocalDateTime.now());
            //todo 加uid 暂时固定
            statistics.setUid(1);
            statisticsService.save(statistics);
        }else{
            for (StaticItem item : dispatcherNums) {
                Integer uid = item.getUid();
                Long count = null == item.getCount() ? 0L : item.getCount();
                Integer onCarTime = null == getDataFromListMap(onCarTimes, uid) ? 0 : getDataFromListMap(onCarTimes, uid);
                Integer onHelpTime = null == getDataFromListMap(onHelpTimes, uid) ? 0 : getDataFromListMap(onHelpTimes, uid);

                Statistics statistics = new Statistics();
                statistics.setTime(count.intValue());
                statistics.setAvgCar(new BigDecimal(onCarTime));
                statistics.setAvgBaseHelp(new BigDecimal(onHelpTime));
                statistics.setMonth(localDate.getMonthValue());
                statistics.setDay(localDate.getDayOfMonth());
                statistics.setYear(localDate.getYear());
                statistics.setUid(uid);
                statistics.setCreateTime(LocalDateTime.now());
                System.out.println(statistics);
                statisticsService.save(statistics);
            }
        }


    }

    private Integer getDataFromListMap(List<StaticItem> list, Integer uid) {
        if (list.isEmpty()) {
            return null;
        }
        for (StaticItem item : list) {
            Integer integer = item.getUid();
            if (uid.intValue() == integer.intValue()) {
                return item.getCount().intValue();
            }
        }
        return null;

    }

}
